package com.whnk.attendance.controller;

import com.whnk.attendance.domain.EmpBaseInfo;
import com.whnk.attendance.service.IEmpBaseInfoService;
import com.whnk.attendance.vo.EmpBaseInfoListVo;
import com.whnk.attendance.vo.EmpBaseInfoQueryVo;
import com.whnk.attendance.vo.EmpBaseInfoUpdateVo;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 人员基本信息Controller
 *
 * @author whnk
 * @date 2021-12-28
 */

@RestController
@RequestMapping("/empBase")
@Api(tags = "人员基本信息表")
public class EmpBaseInfoController extends BaseController {

    @Autowired
    private IEmpBaseInfoService empBaseInfoService;

    /**
     * 分页查询人员基本信息列表
     */
    @PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询人员基本信息列表", notes = "分页查询人员基本信息列表")
    public TableDataInfo list(@ApiParam(value = "人员基本信息", required = false) EmpBaseInfoQueryVo empBaseInfoQueryVo){
        startPage();
        List<EmpBaseInfoListVo> list = empBaseInfoService.selectEmpBaseInfoListVoList(empBaseInfoQueryVo);
        return getDataTable(list);
    }

    /**
     * 根据证件号码查询人员基本信息
     */
    @PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping(value = "/query/{empId}")
    @ApiOperation(value = "根据证件号码查询人员基本信息", notes = "根据证件号码查询人员基本信息")
    public AjaxResult getInfo(@ApiParam(value = "人员基本信息", required = false) @PathVariable("empId") String empId)
    {
        return AjaxResult.success(empBaseInfoService.selectEmpBaseInfoByEmpId(empId));
    }

    /**
     * 新增人员基本信息
     */
    @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "人员基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增人员基本信息", notes = "新增人员基本信息")
    public AjaxResult add(@ApiParam(value = "人员基本信息", required = false) @Validated @RequestBody EmpBaseInfoUpdateVo empBaseInfoUpdateVo)
    {
        if(!empBaseInfoService.checkEmpId(empBaseInfoUpdateVo)){
            return AjaxResult.error("新增证件号码为" + empBaseInfoUpdateVo.getEmpId() + "的人员信息失败，该人员信息已存在");
        }
        return toAjax(empBaseInfoService.insertEmpBaseInfo(empBaseInfoUpdateVo));
    }

    /**
     * 修改人员基本信息
     */
    @PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "人员基本信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改人员基本信息", notes = "修改人员基本信息")
    public AjaxResult edit(@ApiParam(value = "人员基本信息", required = false) @Validated @RequestBody EmpBaseInfoUpdateVo empBaseInfoUpdateVo)
    {
        if(!empBaseInfoService.checkEmpId(empBaseInfoUpdateVo)){
            return AjaxResult.error("修改证件号码为" + empBaseInfoUpdateVo.getEmpId() + "的人员信息失败，该人员信息已存在");
        }
        return toAjax(empBaseInfoService.updateEmpBaseInfo(empBaseInfoUpdateVo));
    }

    /**
     * 批量删除人员基本信息
     */
    @PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "人员基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{empIds}")
    @ApiOperation(value = "批量删除人员基本信息", notes = "批量删除人员基本信息")
    public AjaxResult remove(@ApiParam(value = "证件号码", required = false) @PathVariable String[] empIds)
    {
        return toAjax(empBaseInfoService.deleteEmpBaseInfoByEmpIds(empIds));
    }

    /**
     * 导出人员基本信息
     */
    @PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "人员基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出人员基本信息", notes = "导出人员基本信息")
    public void export(HttpServletResponse response, @ApiParam(value = "人员基本信息", required = false) EmpBaseInfoQueryVo empBaseInfoQueryVo) throws IOException
    {
        List<EmpBaseInfoListVo> list = empBaseInfoService.selectEmpBaseInfoListVoList(empBaseInfoQueryVo);
        ExcelUtil<EmpBaseInfoListVo> util = new ExcelUtil<EmpBaseInfoListVo>(EmpBaseInfoListVo.class);
        util.exportExcel(response, list, "人员基本信息数据");
    }

    /**
     * 导入人员基本信息列表
     */
    @PreAuthorize(hasPermi = "attendance:info:import")
    @Log(title = "人员基本信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入人员基本信息", notes = "导入人员基本信息")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EmpBaseInfoListVo> util = new ExcelUtil<>(EmpBaseInfoListVo.class);
        List<EmpBaseInfoListVo> empBaseInfoList = util.importExcel(file.getInputStream());
        String message = empBaseInfoService.importData(empBaseInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 证件号码id,姓名name接口
     */
    @GetMapping("/empBaseList")
    @ApiOperation(value = "证件号码id,姓名name接口", notes = "证件号码id,姓名name接口")
    public TableDataInfo empBaseList(){
        List<EmpBaseInfoListVo> list = empBaseInfoService.empBaseList();
        return getDataTable(list);
    }
}
